package com.canxi.user.utils;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.canxi.common.core.domain.R;
import com.canxi.common.enums.DeviceType;
import com.canxi.common.enums.UserType;
import com.canxi.common.helper.LoginHelper;
import com.canxi.common.utils.StringUtils;
import com.canxi.common.utils.spring.SpringUtils;
import com.canxi.user.domain.vo.CxUserVo;
import com.canxi.user.service.IUserService;
import com.canxi.user.vo.LoginVo;
import com.canxi.cxcommon.model.CxUser;

/**
 * @Author: canxi
 * @Date: 2022/11/1 11:17
 * @Description:
 * @Package_name:com.canxi.core.config.utils
 */

public class CXLoginUtils {

    public static CxUserVo getUserByToken(){
        try {
            Long userId = LoginHelper.getUserId();
            CxUserVo userVo = SpringUtils.getBean(IUserService.class).queryById(String.valueOf(userId));
            return userVo;
        }catch (Exception e){
            System.out.println("token失效");
        }

        return  null;

    }

    /**
     *
     * * @param wxUser
     * @param //是否需要登录
     */
    public static void refreshUser(CxUserVo userVo, Boolean needLogin, DeviceType deviceType, String loginType){
        CxUser cxUser = BeanUtil.toBean(userVo, CxUser.class);
        cxUser.setUserId(Long.valueOf(userVo.getId()));
        cxUser.setUtype(loginType);
        cxUser.setUsername(userVo.getNickname());
        cxUser.setPhone(userVo.getPhone());
        cxUser.setUserType(UserType.APP_USER.getUserType());
        LoginHelper.loginByDevice(cxUser, deviceType);
    }

    public static R<LoginVo> authMsg(CxUserVo userVo){
        if (StringUtils.isBlank(userVo.getNickname())){
            return R.fail("非首次未授权",new LoginVo(StpUtil.getTokenValue(),userVo,"0"));

        }

        return R.ok("登录成功",new LoginVo(StpUtil.getTokenValue(),userVo,"0"));

    }


}
